﻿// -----------------------------------------------------------------------
// <copyright file="EmailSmsTemplateData.cs" company="VDC-IT" author="LTDUC">
//      Copyright (c) VDC-IT. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

namespace VDC.VONE.DAL
{
    using System;
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using System.Reflection;
    using Microsoft.ApplicationBlocks.Data;
    using VDC.COMMON;
    using VDC.VONE.ENTITIES;
    
    /// <summary>
    /// Class access to db EmailSmsTemplates
    /// </summary>
    public class EmailSmsTemplateData
    {
        /// <summary>
        /// Get EmailSmsTemplate By Id
        /// </summary>
        /// <param name="emailSmsTemplateId">Param emailSmsTemplateId</param>
        /// <returns>Return EmailSmsTemplate</returns>
        public EmailSmsTemplateEN GetEmailSmsTemplateGetById(int emailSmsTemplateId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                EmailSmsTemplateEN emailSmsTemplate = null;
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneEmailSmsTemplateGetById, emailSmsTemplateId);
                while (reader.Read())
                {
                    emailSmsTemplate = new EmailSmsTemplateEN();

                    if (!reader.IsDBNull(0))
                    {
                        emailSmsTemplate.EmailSmsTemplateId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        emailSmsTemplate.Code = reader.GetString(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        emailSmsTemplate.Email = reader.GetString(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        emailSmsTemplate.Sms = reader.GetString(3);
                    }
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return emailSmsTemplate;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Get list all EmailSmsTemplate
        /// </summary>
        /// <returns>Return list all EmailSmsTemplate</returns>
        public IList<EmailSmsTemplateEN> GetEmailSmsTemplates()
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                IList<EmailSmsTemplateEN> lstEmailSmsTemplate = new List<EmailSmsTemplateEN>();
                SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneEmailSmsTemplateGetAll);
                while (reader.Read())
                {
                    EmailSmsTemplateEN emailSmsTemplate = new EmailSmsTemplateEN();

                    if (!reader.IsDBNull(0))
                    {
                        emailSmsTemplate.EmailSmsTemplateId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        emailSmsTemplate.Code = reader.GetString(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        emailSmsTemplate.Email = reader.GetString(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        emailSmsTemplate.Sms = reader.GetString(3);
                    }

                    lstEmailSmsTemplate.Add(emailSmsTemplate);
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return lstEmailSmsTemplate;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Insert to db EmailSmsTemplate
        /// </summary>
        /// <param name="emailSmsTemplate">Paarm EmailSmsTemplate</param>
        /// <returns>Insert EmailSmsTemplate</returns>
        public int Insert(EmailSmsTemplateEN emailSmsTemplate)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.EmailSmsTemplate.Code, emailSmsTemplate.Code),
                                    new SqlParameter(Constants.SqlParamerterName.EmailSmsTemplate.Email, emailSmsTemplate.Email),
                                    new SqlParameter(Constants.SqlParamerterName.EmailSmsTemplate.Sms, emailSmsTemplate.Sms)
                                };

                var result = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneEmailSmsTemplateInsert, sqlParameters));
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return result;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Update to db EmailSmsTemplate
        /// </summary>
        /// <param name="emailSmsTemplate">Param EmailSmsTemplate</param>
        public void Update(EmailSmsTemplateEN emailSmsTemplate)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.EmailSmsTemplate.EmailSmsTemplateId, emailSmsTemplate.EmailSmsTemplateId),
                                    new SqlParameter(Constants.SqlParamerterName.EmailSmsTemplate.Code, emailSmsTemplate.Code),
                                    new SqlParameter(Constants.SqlParamerterName.EmailSmsTemplate.Email, emailSmsTemplate.Email),
                                    new SqlParameter(Constants.SqlParamerterName.EmailSmsTemplate.Sms, emailSmsTemplate.Sms)
                                };

                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneEmailSmsTemplateUpdate, sqlParameters);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Delete EmailSmsTemplate by emailSmsTemplateId
        /// </summary>
        /// <param name="emailSmsTemplateId">param emailSmsTemplateId</param>
        public void Delete(int emailSmsTemplateId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[] 
                { 
                    new SqlParameter(Constants.SqlParamerterName.EmailSmsTemplate.EmailSmsTemplateId, emailSmsTemplateId)
                };

                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneEmailSmsTemplateDelete, sqlParameters);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Gets the email SMS template get by code.
        /// </summary>
        /// <param name="code">The code.</param>
        /// <returns>return EmailSmsTemplateEN</returns>
        public EmailSmsTemplateEN GetEmailSmsTemplateGetByCode(string code)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                EmailSmsTemplateEN emailSmsTemplate = null;
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneEmailSmsTemplateGetByCode, code);
                while (reader.Read())
                {
                    emailSmsTemplate = new EmailSmsTemplateEN();

                    if (!reader.IsDBNull(0))
                    {
                        emailSmsTemplate.EmailSmsTemplateId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        emailSmsTemplate.Code = reader.GetString(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        emailSmsTemplate.Email = reader.GetString(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        emailSmsTemplate.Sms = reader.GetString(3);
                    }
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return emailSmsTemplate;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }
    }
}